package com.fh.adminrole.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fh.adminrole.entity.UmsAdminRole;
import com.fh.adminrole.mapper.UmsAdminRoleMapper;
import com.fh.adminrole.service.IUmsAdminRoleService;
import com.fh.search.AdminRoleBO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author F
 * @since 2021-05-13
 */
@Service
public class UmsAdminRoleServiceImpl extends ServiceImpl<UmsAdminRoleMapper, UmsAdminRole> implements IUmsAdminRoleService {

    @Override
    public void saveRoleToAdmin(AdminRoleBO adminRoleBO) {
        //删除用户关联的角色信息
        QueryWrapper<UmsAdminRole> queryWrapper = new QueryWrapper<UmsAdminRole>();
        queryWrapper.eq("admin_id", adminRoleBO.getUserId());
        remove(queryWrapper);
        if (!CollectionUtils.isEmpty(adminRoleBO.getRoleIdList())) {
            List<UmsAdminRole> adminRoleList = new ArrayList<>();
            adminRoleBO.getRoleIdList().forEach(roleId -> {
                UmsAdminRole umsAdminRole = new UmsAdminRole();
                umsAdminRole.setAdminId(adminRoleBO.getUserId());
                umsAdminRole.setRoleId(roleId);
                adminRoleList.add(umsAdminRole);
            });
            saveBatch(adminRoleList);
        }
    }
}
